import $platform from './platform';
import $api from './request';
import {
	router as $Router
} from '@/router/index';

/**
 * 支付
 */
export default class XYVENUEPay {

	constructor(payment, order, orderType) {
		this.payment = payment;
		this.order = order;
		// console("this.order:"+this.order);
		this.orderType = orderType;
		this.platform = $platform.get();
		this.payMehod = this.getPayMethod();
	}

	getPayMethod() {
		var payMethod = {
			'wxMiniProgram': {
				'wechat': async () => {
					return new Promise((resolve, reject) => {
						resolve(this.wxMiniProgramPay());
					})

				},
				'balance': async () => {
					return new Promise((resolve, reject) => {
						resolve(this.balancePay());
					})
				}
			},
		}
		return payMethod[this.platform][this.payment];
	}

	// 预支付
	prepay() {
		var that = this;
		return new Promise((resolve, reject) => {
			var params = {
				order_sn: that.order.order_sn,
				payment: that.payment,
				order_type: that.orderType,
			}
			$api.post({
				url: '/pay/prepay',
				loadingTip: '支付中...',
				data: params,
				success: res => {
					resolve(res);
				},
				fail(res) {
					reject(res);
				}
			});
		});
	}

	// 余额支付
	async balancePay() {
		var that = this;
		return new Promise((resolve, reject) => {
			that.prepay().then((res) => {
				resolve('success')
			}, (res) => {
				reject('fail');
			});
		});
	}

	async myClick() {
		return new Promise((resolve, reject) => {
			// console.log("order:"+JSON.stringify(this.order, null, 2));
			$api.post({
				url: '/recharge_order/add',
				data: {
					store_id: this.store.id,
					recharge_id: this.recharge_id,
					pay_type: pay_type,
					ismoney: this.ismoney,
					money: this.money
				},
				success: res => {
					resolve(res);
					$api.post({
						// url: '/Shande/test',
						url: '/Shande/recharge',
						data: {
							orderjson: this.order
						},
						success: res => {
							resolve(res);
						},
						fail: err => {
							reject(err);
						}
					});
				},
				fail: err => {
					reject(err);
				}
			});
		});
	}



	// 微信小程序支付
	async wxMiniProgramPay() {
		var result = await this.myClick();
		// console.log("result:", result);
		const jsonData = JSON.parse(result.data);
		const payDataString = jsonData.pay_data; // 获取 pay_data 的 JSON 字符串
		const payData = JSON.parse(payDataString); // 解析成对象
		return new Promise((resolve, reject) => {
			uni.requestPayment({
				appId: payData.appId,
				nonceStr: payData.nonceStr,
				package: payData.package,
				paySign: payData.paySign,
				signType: payData.signType,
				timeStamp: payData.timeStamp,
				success: (e) => {
					this.payResult("success");
				},
				fail: (e) => {
					uni.showModal({
						content: "支付失败,原因为: " + e.errMsg,
						showCancel: false
					})
				}
			})
		});
	}

	// 支付结果跳转
	payResult(resultType) {
		$Router.replace({
			path: '/pages/user/order/payresult',
			query: {
				orderId: this.order.id,
				type: this.payment,
				payment: this.payment,
				payState: resultType,
				orderType: this.orderType
			}
		});
	}

}